package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsAdminServiceImpl implements DtsAdminService {
    @Autowired
    DtsAdminMapper adminMapper;
    @Autowired
    DtsBrandMapper brandMapper;

    /**
     * 通过用户名查找
     * @param username
     * @return
     */
    @Override
    public DtsAdmin findByUsername(String username) {
        //创建查询条件
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getUsername,username);
        //执行查询操作
        DtsAdmin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    /**
     * 查找每页
     * @param page
     * @param limit
     * @param username
     * @param sort
     * @param order
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String username, String sort, String order) {
        IPage<DtsAdmin> pageOne = new Page<>(page,limit);
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
            wrapper.eq("username",username);
        }
        //判断order升序或降序
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }

        //查询每页
        adminMapper.selectPage(pageOne,wrapper);
        //将查询到的记录以集合形式返回
        List<DtsAdmin> list = pageOne.getRecords();
        list.stream().map(admin ->{
            //????
            admin.setPassword("");
            return admin;
        }).collect(Collectors.toList());

        //创建返回值对象
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageOne.getTotal());
        map.put("items",list);

        return map;
    }

    @Override
    public void insert(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);

        //密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        String encode = passwordEncoder.encode(admin.getPassword());

        admin.setPassword(encode);

        adminMapper.insert(admin);
    }

    @Override
    public void update(DtsAdmin admin) {
        admin.setAddTime(new Date());

        //密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        String encode = passwordEncoder.encode(admin.getPassword());

        admin.setPassword(encode);

        adminMapper.updateById(admin);
    }

    @Override
    public void delete(DtsAdmin admin) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(DtsBrand::getAdminId,admin.getId());
        wrapper.eq(DtsBrand::getDeleted,false);

        Integer count = brandMapper.selectCount(wrapper);
        if (count > 0){
            throw  new RuntimeException("已关联，不能删除");
        }

        admin.setDeleted(true);
        adminMapper.updateById(admin);
    }
}
